import sys
import os.path 

cur_path = os.path.dirname(__file__)
parent_path = os.path.abspath(os.path.join(cur_path, '..'))
sys.path.append(parent_path)

import math
import pymongo
from topapi import *
from exceptions import Exception

def store2db(db, trades):
    for t in trades:
        t['_id'] = t['tid']
        db.trades.save(t)

def get_trades_and_store_to_db(db, user):
    trade = Trade()
    page_no = 1
    page_size = 100
    trades_info = trade.sold_get(user['top_session'], page_no=page_no, page_size=page_size)
    total_results = trades_info['total_results']
    trades = trades_info['trades']['trade']
    page_num = int(math.ceil(1.0 * total_results / page_size))
    store2db(db, trades)
    for page_no in range(2, page_num+1):
        trades_info = trade.sold_get(user['top_session'], page_no=page_no, page_size=page_size)
        trades = trades_info['trades']['trade']
        store2db(db, trades)


def traverse_user_and_get_trades():
    m = pymongo.Connection('127.0.0.1:57324')
    db = m.taogold
    skip = 120
    limit = 100
    while True:
        print "skip = %d, limit = %d" %(skip, limit)
        cur = db.user.find(fields = ['top_session', ], timeout=False).skip(skip).limit(limit)
        if cur.count(True) <=0:
            break
        for u in cur:
            for retry in range(3):
                try:
                    get_trades_and_store_to_db(db, u)
                    print 'processed: %s' %(u['_id'].encode('utf8'))
                    break
                except Exception as e:
                    print 'get trades and store to db fail: %d, %s, %s' %(retry, u['_id'].encode('utf8'), repr(e))
                    continue 
        skip += limit
        

if __name__ == '__main__':
    traverse_user_and_get_trades()



